import { IFlexLayoutElement } from "./IElement.js";
import { ICharacter, GetIconCharacterByIconName } from "./ICharacter.js";

// 文本字符
export class ITextCharacter extends ICharacter {
    constructor(is_character_icon) {
        super();
        this.SetTagName("ITextCharacter");
        this.m_is_character_icon = is_character_icon;
    }

    SetValue(value) {
        this.m_is_character_icon = this.m_is_character_icon || value.length > 1;
        if (this.m_is_character_icon) {
            this.SetCharacter(GetIconCharacterByIconName(value));
        } else {
            this.SetCharacter(value);
        }
    }

    UpdateLayout() {
        const parent = this.GetParent();
        if (parent) {
            this.SetFontColor(parent.GetFontColor());
            this.SetBackgroundColor(undefined); // 文本字符不支持背景 背景由文本自身绘制
            // this.SetBackgroundColor(parent.GetBackgroundColor());

            const parent_font_size = parent.GetFontSize();
            const parent_font_family = parent.GetFontFamily();
            const parent_line_height = parent.GetLineHeight();

            if (parent_font_size != this.m_font_size) {
                this.m_font_size = parent_font_size;
                this.SetLayoutChanged(true);
            }
            if (parent_line_height != this.m_line_height) {
                this.m_line_height = parent_line_height;
                this.SetLayoutChanged(true);
            }
            if (!this.m_is_character_icon && parent_font_family != this.m_font_family) {
                this.m_font_family = parent_font_family;
                this.SetLayoutChanged(true);
            }
        }
        super.UpdateLayout();
    }

    OnMouseDown(event) {
        const parent = this.GetParent();
        if (parent && parent.OnMouseDown) {
            parent.OnMouseDown(event);
        }
    }

    OnMouseMove(event) {
        const parent = this.GetParent();
        if (parent && parent.OnMouseMove) {
            parent.OnMouseMove(event);
        }
    }

    OnMouseUp(event) {
        const parent = this.GetParent();
        if (parent && parent.OnMouseUp) {
            parent.OnMouseUp(event);
        }
    }

    OnClick(event) {
        const parent = this.GetParent();
        if (parent && parent.OnClick) {
            parent.OnClick(event);
        }
    }

    OnContextMenu(event) {
        const parent = this.GetParent();
        if (parent && parent.OnContextMenu) {
            parent.OnContextMenu(event);
        }
    }
}

// 文本元素
// 1 继承弹性布局元素， 实现文本布局
// 2 将文本转换成字符子元素， 实现文本显示
export class IText extends IFlexLayoutElement {
    constructor() {
        super();
        this.SetTagName("IText");
        this.SetFontSize(this.GetThemeFontSize());
        this.SetLineHeight(this.GetThemeLineHeight());
        this.SetFontFamily(this.GetThemeFontFamily());
        this.SetFontColor(this.GetThemeFontColor());
        // hover style
        this.SetSupportHover(false);
        this.SetHoverFontColor(this.GetThemePrimaryTextColor());
        this.SetHoverBackgroundColor(this.GetThemeHoverBackgroundColor());
        this.SetLastFontColor(this.GetFontColor());
        this.SetLastBackgroundColor(this.GetBackgroundColor());

        this.SetAttributeSetterGetter("value", (value) => { this.SetText(value); });
        this.SetAttributeSetterGetter("align", (value) => { this.SetStyleJustifyContent(value); })
        this.SetAttributeSetterGetter("font-size", (value) => { this.SetFontSize(parseFloat(value)); });
        this.SetAttributeSetterGetter("font-family", (value) => { this.SetFontFamily(value); });
        this.SetAttributeSetterGetter("line-height", (value) => { this.SetLineHeight(parseFloat(value)); });

        this.SetAttributeSetterGetter("type", (value) => {
            if (value == "primary") {
                this.SetFontColor(this.GetThemePrimaryColor());
            }
            else if (value == "success") {
                this.SetFontColor(this.GetThemeSuccessColor());
            }
            else if (value == "warning") {
                this.SetFontColor(this.GetThemeWarningColor());
            }
            else if (value == "danger") {
                this.SetFontColor(this.GetThemeDangerColor());
            }
            else if (value == "info") {
                this.SetFontColor(this.GetThemeInfoColor());
            }
            else {
                this.SetFontColor(this.GetThemePrimaryTextColor());
            }
        });

        this.SetAttributeSetterGetter("size", (value) => {
            if (value == "large") {
                this.SetFontSize(this.GetThemeLargeFontSize());
            }
            else if (value == "small") {
                this.SetFontSize(this.GetThemeSmallFontSize());
            }
            else {
                this.SetFontSize(this.GetThemeFontSize());
            }
        });
    }

    GetText() { return this.m_text; }
    SetSupportHover(support_hover) { this.m_support_hover = support_hover; }
    IsSupportHover() { return this.m_support_hover; }
    SetHoverFontColor(hover_font_color) { this.m_hover_font_color = hover_font_color; }
    GetHoverFontColor() { return this.m_hover_font_color; }
    SetHoverBackgroundColor(hover_background_color) { this.m_hover_background_color = hover_background_color; }
    GetHoverBackgroundColor() { return this.m_hover_background_color; }
    SetLastFontColor(last_font_color) { this.m_last_font_color = last_font_color; }
    GetLastFontColor() { return this.m_last_font_color; }
    SetLastBackgroundColor(last_background_color) { this.m_last_background_color = last_background_color; }
    GetLastBackgroundColor() { return this.m_last_background_color; }

    SetFontSize(font_size) {
        if (this.m_font_size === font_size) {
            return;
        }
        this.m_font_size = font_size;
        this.MarkLayoutChanged();
    }

    SetFontFamily(font_family) {
        if (this.m_font_family === font_family) {
            return;
        }
        this.m_font_family = font_family;
        this.MarkLayoutChanged();
    }

    SetLineHeight(line_height) {
        if (this.m_line_height === line_height) {
            return;
        }
        this.m_line_height = line_height;
        this.MarkLayoutChanged();
    }

    SetText(text) {
        if (this.m_text === text) {
            return;
        }
        const window = this.GetWindow();
        const root = window.GetRoot();
        const parent = this;
        const childrens = [];
        for (let i = 0; i < text.length; i++) {
            const character = new ITextCharacter();
            character.SetCharacter(text[i]);
            character.SetWindow(window);
            character.SetRoot(root);
            character.SetParent(parent);
            character.SetSupportStyle(false);
            character.SetLayoutChanged(true);
            childrens.push(character);
        }
        this.m_childrens = childrens;
        this.m_text = text;
        this.MarkLayoutChanged();
        this.Refresh();
    }

    OnMouseEnter() {
        if (!this.IsSupportHover()) return;
        super.OnMouseEnter();

        // 备份旧样式
        this.SetLastFontColor(this.GetFontColor());
        this.SetLastBackgroundColor(this.GetBackgroundColor());
        // 设置悬浮样式
        this.SetFontColor(this.GetHoverFontColor());
        this.SetBackgroundColor(this.GetHoverBackgroundColor());
        // 刷新元素
        this.Refresh();
    }

    OnMouseLeave() {
        if (!this.IsSupportHover()) return;
        super.OnMouseLeave();

        // 恢复旧样式
        this.SetFontColor(this.GetLastFontColor());
        this.SetBackgroundColor(this.GetLastBackgroundColor());
        // 刷新元素
        this.Refresh();
    }
}

export class Text extends IText {
    constructor() {
        super();
        this.SetTagName("Text");
    }
}